package geppetto.cat.analysis;

import geppetto.cat.corpus.BilingualCorpus;
import gnu.trove.TIntObjectHashMap;

import java.io.PrintStream;



public class AllErrors {

	TIntObjectHashMap<ErrorByForeignWord> _errorsByForeingWord;

	SentenceErrors[] _sentenceErrors;

	public AllErrors(int nrSentences) {
		_sentenceErrors = new SentenceErrors[nrSentences];
		_errorsByForeingWord = new TIntObjectHashMap<ErrorByForeignWord>();
	}

	public void addSentenceError(int sentenceNr, SentenceErrors errors) {
		_sentenceErrors[sentenceNr] = errors;
		for (int i = 0; i < errors.getSize(); i++) {
			AlignmentError error = errors.getError(i);
			if (error != null) {
				ErrorByForeignWord errorF = _errorsByForeingWord
						.get(error._wordId);
				if (errorF == null) {
					errorF = new ErrorByForeignWord(error._wordId);
				}
				errorF.addError(error);
				_errorsByForeingWord.put(error._wordId, errorF);
			}
		}
	}

	public void output(BilingualCorpus corpus, PrintStream out) {
		// Print errors by sentence
		for (int i = 0; i < _sentenceErrors.length; i++) {
			if (_sentenceErrors[i] != null)
				_sentenceErrors[i].output(corpus, out);
		}

		// Should sort global errors and only print above a treshold

		// Print global errors
		for (int i = 0; i < _errorsByForeingWord.size(); i++) {
			ErrorByForeignWord error = _errorsByForeingWord.get(i);
			if (error != null)
				if (error._nrErrors > 5)
					error.output(corpus, out);
		}
	}

}
